package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.domain.request.LoginRequest;
import edu.sdjzu.exam.domain.request.RegisterRequest;
import edu.sdjzu.exam.domain.request.UpdateUserRequest;
import edu.sdjzu.exam.service.auth.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    public CommonResponse<Object> login(@RequestBody @Validated LoginRequest request) {
        return authService.login(request.getUsername(), request.getPassword());
    }

    @PostMapping("/logout")
    public CommonResponse<String> logout() {
        return authService.logout();
    }

    @PostMapping("/register")
    public CommonResponse<String> register(@RequestBody @Validated RegisterRequest request) {
        return authService.register(request.getUsername(), request.getPassword(), request.getEmail(), request.getRealname());
    }

    @GetMapping("/userinfo")
    @SaCheckLogin
    public CommonResponse<Object> getUserInfo() {
        return authService.getUserInfo();
    }
    
    @PostMapping("/update")
    @SaCheckLogin
    public CommonResponse<String> updateUser(@RequestBody @Validated UpdateUserRequest request) {
        return authService.updateUser(request.getEmail(), request.getRealname(), request.getPassword());
    }
}
